import Vue from 'vue'
import Router from 'vue-router'
import Welcome from '../views/Welcome.vue' // 首页
import FrontStage from '../views/layout/frontstage'
// import mobileLayout from '../views/mobile/user/index.vue'
// import BackStage from './views/layout/backstage'
// 导入相关路由模块
// 客户端
import userRouter from './user'
// 员工端
import workerRouter from './worker'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  scrollBehavior: () => ({
    y: 0
  }),
  routes: [
    // 登陆界面
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login')
    },
    {
      // 页面重定向到页面的首页
      path: '/',
      redirect: '/welcome',
      component: FrontStage,
      children: [{
        path: '/welcome',
        name: 'Welcome',
        component: Welcome
      },
      // 客户端模块
      userRouter,
      // 员工模块
      workerRouter
      ]
    },
    // 移动端
    {
      path: '/mobile',
      name: 'Mobile',
      redirect: '/mobile/library',
      component: () => import('../views/mobile/user/index.vue'),
      children: [
        // 客户图库
        {
          path: 'library',
          name: 'Library',
          component: () => import('../views/mobile/user/library.vue')
        },
        // 客户图片
        {
          path: 'photo',
          name: 'Photo',
          component: () => import('../views/mobile/user/photo.vue')
        },
        // 客户意见
        {
          path: 'masker',
          name: 'Masker',
          component: () => import('../views/mobile/user/masker.vue')
        },
        // 个人中心
        {
          path: 'center',
          name: 'Center',
          component: () => import('../views/mobile/user/center.vue')
        },
        // 登陆
        {
          path: 'login',
          name: 'Login',
          component: () => import('../views/mobile/user/login')
        }
      ]
    },
    {
      // 页面重定向到页面的首页
      path: '/backstage',
      redirect: '/backstage/welcome',
      component: () => import('../views/layout/backstage/index.vue'),
      // component: Backstage
      children: [{
        path: 'welcome',
        name: 'Welcome',
        component: () => import('../views/layout/backstage/Welcome.vue')
      },
      {
        path: 'users',
        component: () => import('../views/layout/backstage/user/Users.vue')
      },
      {
        path: 'rights',
        component: () => import('../views/layout/backstage/power/Rights.vue')
      },
      {
        path: 'roles',
        component: () => import('../views/layout/backstage/power/Roles.vue')
      },
      {
        path: 'categories',
        component: () => import('../views/layout/backstage/goods/Cate.vue')
      },
      {
        path: 'params',
        component: () => import('../views/layout/backstage/goods/Params.vue')
      },
      {
        path: 'goods',
        component: () => import('../views/layout/backstage/goods/List.vue')
      },
      {
        path: 'goods/add',
        component: () => import('../views/layout/backstage/goods/Add.vue')
      },
      {
        path: 'orders',
        component: () => import('../views/layout/backstage/order/Order.vue')
      },
      {
        path: 'reports',
        component: () => import('../views/layout/backstage/report/Report.vue')
      }
      ]
    }
  ]

})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  // console.log(document.documentElement.clientWidth || document.body.clientWidth, "视口宽度")
  var width = document.documentElement.clientWidth || document.body.clientWidth
  if (width < 540) {
    if (to.path === '/mobile/login') return next()
    // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/mobile/login')
    next()
  } else {
    if (to.path === '/login') return next()
    // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
  }
})

export default router
